package main

import (
	"encoding/json"
	"fmt"
	"log"
)

type TestJson struct {
	Title   string
	Content string
	Author  string
	Views   int
}

func main() {
	// 编码
	epost := TestJson{
		Title:   "Exploring Go's Standard Library",
		Content: "Go's standard library is vast...",
		Author:  "Jane Doe",
		Views:   3490,
	}

	jsonBytes, err := json.Marshal(epost)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("JSON encoding:", string(jsonBytes))

	// 解码
	var dpost TestJson
	err = json.Unmarshal(jsonBytes, &dpost)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Blog post:", dpost)
}
